package com.example.demo.service.impl;

import com.example.demo.Utils.TimeUtils;
import com.example.demo.bean.Caculater;
import com.example.demo.mapper.CaculateMapper;
import com.example.demo.service.CaculateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.DateUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CaculateServiceImpl implements CaculateService {

    @Autowired
    CaculateMapper caculateMapper;


    @Override
    public int addBuySum() {
        return 0;
    }

    @Override
    public int caddBuySum() {
        float sumMoney = caculateMapper.caculateSumMoney();
        Caculater caculater = new Caculater();
        caculater.setMoney(sumMoney);
        caculater.setDate(TimeUtils.getNowTime());

        return caculateMapper.addBuySum(caculater);
    }

    @Override
    public Map queryMoneyOfYear(String year) {
        Map sumMap = new HashMap<String,Object>();
        float yearMomey = caculateMapper.queryMoneyOfYear(year);
        List<Caculater> monthMoney = caculateMapper.queryMoneyOfMonth(year);
        monthMoney.stream().map(e ->{e.setDate(e.getDate().substring(5));return e;}).collect(Collectors.toList());
        sumMap.put("yearSum",yearMomey);
        sumMap.put("monthMoney",monthMoney);
        return  sumMap;
    }


    @Override
    public List queryEveryTimesOfMonth(Integer year,Integer month) {
        return caculateMapper.queryEveryTimesOfMonth(year,month);
    }

    @Override
    public boolean hadAddBuySum() {
        String nowTime = TimeUtils.getNowTime();
        String thisWeekStartTime = TimeUtils.getThisWeekStart();
        float thisWeekBuySum = caculateMapper.queryThisWeekBuySum(nowTime.substring(8),thisWeekStartTime.substring(8));
        return thisWeekBuySum == 0.0 ;
    }
}
